

#include "../sc-s/main.h"
#include "../sc-s/lcd.h"
#include "../sc-s/sc-s.h"
#include "../sc-s/utils.h"
#include "../sc-s/trans-s.h"

#include <stdlib.h>


#define HR_ZPB	15		// 1.5%
#define HR_ZNB	25		//-2.5%

#define LR_ZPB	9		// 0.09%
#define LR_ZNB	20		//-2.00%

u16 meas = 0;


#define DEFAULT_AWP_DELAY	45
#define INTRO_DELAY	1000

#define SG_LE	170
#define SG_HE	220

void display_name()
{
	lcd_print(" SC-O2  ");
}


void display_alarms()
{
	char str[8];
	lcd_home();
	lcd_print("AL1:");
	fuse1(str,AL1);
	lcd_print(str);
	WAITMS(INTRO_DELAY);

	lcd_home();
	lcd_print("AL2:");
	fuse1(str,AL2);
	lcd_print(str);
	WAITMS(INTRO_DELAY);
}


void sr_low()
{
	lcd_home();

	if (!MIN && calib_ptr_deep != 2)
	{
		calib_ptr_deep = 0;
		tp_addr = P_ADDR_RESET_DEFAULTS;
		return;
	}
	 
	switch(calib_ptr_deep)
	{
		case 0x01:
		{
			lcd_print("ZER:");
			show_med_low(meas);
			
			if (!ENT)
			{
				tp_addr = P_ADDR_SCALLZ;
				lcd_home();
				lcd_print("UST.0.00%");
				while(!ENT);
				WAITMS(1000);
				calib_ptr_deep++;
				calib_gas = read_word(EEP_STN);
			}
			break;
		}
		case 0x02:
		{
			lcd_print("WZOR");
			show_med_low(calib_gas);
			
			if (!MIN)
			{
				calib_gas --;
				if (calib_gas < SG_LE)
				calib_gas = SG_LE;
			}
			if (!PLU)
			{
				calib_gas ++;
				if (calib_gas > SG_HE)
				calib_gas = SG_HE;
			}
			if(!ENT)
			{
				write_word(EEP_STN,calib_gas);
				calib_ptr_deep++;
				while(!ENT);
				break;
			}
			
			break;
		}
		case 0x03:
		{
			lcd_print("CZN:");
			show_med_low(meas & 0x3FFF);
			
			if (!ENT)
			{
				tp_data = calib_gas;
				tp_addr = P_ADDR_SCALLS;
				lcd_home();
				lcd_print("Ustaw.  ");
				WAITMS(2000);
				calib_ptr_deep = 0;
				return;
			}
			break;
		}
	}
}



#define SZ_MINUS	400		// +- 4.00% O2
#define SZ_PLUS		400
#define GAS_MAX		220
#define GAS_MIN		180
#define SC_PLUS		600
#define SC_MINS		600

void cal_low()
{
	char str[8];
		lcd_home();

	switch (calib_ptr_deep)
	{
		case 0x01:
		{
			if (v_tmp & 0x8000)
			{
				lcd_print("T:-");
			}
			else
			{
				lcd_print("T: ");
			}
			fuse1(str,v_tmp & 0x7FFF);
			lcd_print(str);
			lcd_print("  ");
			WAITMS(1000);
			break;
		}		
		case 0x02:
		lcd_print("POD.AZOT");
		wait_deliver_P();
		tp_addr = P_ADDR_L_RESET;
		wait_deliver_P();
		WAITMS(1500);
		
		calib_ptr_deep++;
		break;
		case 0x03:

		show_media(meas,0x80);		// show spec
		if (!ENT)
		{
			if (meas & 0x1000)
			{
				if ((meas & 0x0FFF) > SZ_MINUS)	// 
				{
					kalib_serwis(0,(1<<14));
					// TODO : Write serwis Z to K
					return;
				}
			}
			else
			{
				if ((meas & 0x0FFF) > SZ_PLUS)	// 
				{
					kalib_serwis(0,(1<<14));
					// TODO : Write serwis Z to K
					return;
				}
				
			}
			p_ack = 0;
			tp_addr = P_ADDR_CALLZ;
			
			tk_data = (1 << 14);
			tk_addr = K_ADDR_LZ;
			tk_repeat = 3;
			// TODO : WRITE ZERO IN K
			
			calib_ptr_deep++;
			lcd_home();
			lcd_print("UST0.00%");
			wait_deliver_P();
			WAITMS(1000);
			lcd_home();
			lcd_print(" ZAPIS! ");
			WAITMS(1000);
		}
		break;
		case 0x04:
		lcd_print(" UST.GAZ");
		WAITMS(1000);
		calib_ptr_deep++;
		
		calib_gas = read_word(EEP_STN);
		break;
		
		case 0x05:
		itoa(calib_gas,str,10);
		lcd_print("GAZ:");
		lcd_print(str);
		lcd_print(" ");
		
		if (!PLU)
		{
			calib_gas++;
			if (calib_gas > GAS_MAX)
			calib_gas = GAS_MAX;
		}
		
		if (!MIN)
		{
			calib_gas--;
			if (calib_gas < GAS_MIN)
			calib_gas = GAS_MIN;
		}
		
		if (!ENT)
		{
			calib_ptr_deep++;
			lcd_home();
			lcd_print("POD.GAZ.");
			
			write_word(EEP_STN,calib_gas);
			
			// TODO : WRITE GAS TO P & K
			
			
			WAITMS(1000);
		}
		break;
		
		case 0x06:
		
		show_media(meas,0x80);
		

		
		if (!ENT)
		{
			if (meas & 0x1000)	// 12bit
			{
				kalib_serwis(1,(calib_gas | (1 << 14)));
				return; 
			}
			else
			{
				if (((meas & 0x0FFF) < calib_gas - SC_PLUS) || ((meas & 0x0FFF) > calib_gas + SC_MINS))
				{
					kalib_serwis(1,(calib_gas | (1 << 14)));
					return;
				}
			}
			tp_data = calib_gas;
			tp_addr = P_ADDR_CALLS;
			
			tk_data = calib_gas | (1 << 14);
			tk_addr = K_ADDR_LS;
			tk_repeat = 3;
			/// TODO: WRITE KALIB OK TO P
			/// TODO: WRITE KALIB OK TO K
			
			lcd_home();
			lcd_print("UST");
			fuse1(str,calib_gas);
			lcd_print(str);
			lcd_print("%");
			WAITMS(2000);
			calib_ptr_deep = 0;
			calib_ptr = CM_WYJ;

		}
		
	}
	
	
}



void awp_check()
{
	static u8 delay1 = 0;
	static u8 delay2 = 0;
	
	if (!Awp)
	{
		if (awp_flags & POWERFLAG)
		{
			ALARM1_OFF;
			ALARM2_OFF;
			return;
		}
		 
		if (awp_flags & AL1_FLAG)
		{
			ALARM1_OFF;
		}
		else
		{
			ALARM1_ON;
		}
		
		if (awp_flags & AL2_FLAG)
		{
			ALARM2_OFF;
		}
		else
		{
			ALARM2_ON;
		}
		
		return;
	}
	
	
	u8 ok1 = 1;
	u8 ok2 = 1;
	
	if (meas & 0x4000)	// low level
	{
		if (meas & 0x1000)	//minus
		{
			if ((meas & 0x0FFF) > 20)		 //< -20ppm
			{
				ALARM1_OFF;
				ALARM2_OFF;
				return;
			}
		}
		if ((meas & 0x0FFF) < AL1)
		{
			ok1 = 0;
			delay1 = DEFAULT_AWP_DELAY;
		}
		else
		{
			if (delay1)
			{
				delay1 --;
				ok1 = 0;
			}
			if (delay2)
			{
				delay2 --;
				ok2 = 0;
			}
		}
		if ((meas & 0x0FFF) < AL2)
		{
			ok2 = 0;
			ok1 = 0;
			delay2 = DEFAULT_AWP_DELAY;
			delay1 = DEFAULT_AWP_DELAY;
		}
		else
		{
			if (delay2)
			{
				delay2 --;
				ok2 = 0;
			}
		}
		
		if (awp_flags & POWERFLAG)
		{
			ok1 = 0;
			ok2 = 0;
		}
		
		if (ok1 && (!(awp_flags & AL1_FLAG)))
		{
			ALARM1_ON;
		}
		else
		{
			ALARM1_OFF;
		}
		
		if (ok2 && (!(awp_flags & AL2_FLAG)))
		{
			ALARM2_ON;
		}
		else
		{
			ALARM2_OFF;
		}
	}
	
}



void show_high(u16 value, u8 flags)
{
	lcd_home();
	lcd_print("ERR_WYS!");
	return;
}

void show_low(u16 value, u8 flags)
{
	lcd_home();
	char str[10];
	if (! ( value & 0x1000))	// 12 = 0, positiv
	{
	
		value &= 0x0FFF;
		fuse1(str,value);
		
		if (value > 300  && (!(flags & 0x80) ) && MIN)
		{
			lcd_print(">MAX %O2");
			return;
		}
		
		if (value < 100 )
		{
			lcd_print(" ");
		}

		lcd_print(str);
		lcd_print(" %O2");
		return;
	}
	// negativ
	value &= 0x0FFF;		// clear bit 12


	fuse1(str,value);
	if (value > 0)
	{
		lcd_print("-");
	}
	else
	{
		lcd_print(" ");
	}
	lcd_print(str);
	if (value < 100)
	{
		lcd_print(" ");
	}
	lcd_print(" %O2");

	return;
	
}

void show_med_low(u16 value)
{
	char str[10];
	if (! ( value & 0x1000))	// 12 = 0, positiv
	{
		value &= 0x0FFF;
		fuse1(str,value);

		lcd_print(str);
		lcd_print("  ");
		return;
	}
	// negativ
	value &= 0x0FFF;		// clear bit 12
	
	
	fuse1(str,value);
	if (value > 0)
	{
		lcd_print("-");
	}
	lcd_print(str);

	lcd_print("  ");
	return;
	
}


void show_med_high(u16 value)
{
	lcd_print("ERR");
}


void show_media(u16 value, u8 flags)
{
	lcd_home();
	
	if (debug_data)
	{
		lcd_print("D:");
		char str[10];
		itoa(debug_data,str,10);
		lcd_print(str);
		lcd_print("  ");
		WAITMS(250);
		debug_data = 0;
		return;
	}
	
	

	if (value & 0x4000)
	{
		value &= 0xBFFF;	// 14
		show_low(value,flags);
	}
}



void calc_dac_out()
{
	if (!uout)
	{
		napb = 0;
		napa = 0;
		return;
	}
	if (uout == 2)	// in calib;
	return;
	
	u16 tmp = meas;
	float fnap;
	
	
	if (tmp & 0x4000)	// 14bit
	{
		tmp &= 0x3FFF;
		
		if (tmp & 0x1000)		// 12bit  - minus
		{
			tmp &= 0x0FFF;
			
			napa = U1_35;
			napb = U2_35;
			return;
		}
		
		if (tmp > 250)
		{
			napa = U1_21;
			return;
		}
		
		fnap = ( U1_20 - U1_40 ) / (float)250;
		fnap = fnap * tmp ;
		napa = fnap + U1_40;

	}
	
	
}

void config_menu()
{
	char str[8];
	
	switch(calib_ptr_deep)
	{
		case 0x00:
		{
			lcd_home();
			lcd_print(" KONFIG ");
			WAITMS(1000);
			lcd_home();
			lcd_print("SET[-/+]");
			WAITMS(1000);
			calib_ptr_deep++;
			break;
		}
		case 0x01:
		{
			lcd_home();
			switch(Ksp)
			{
				case 0:
				lcd_print(" BRAK   ");
				break;
				case 1:
				lcd_print(" KSP-2  ");
				break;
				case 2:
				lcd_print(" KSP-3  ");
				break;
				
			}
			if (!PLU)
			{
				Ksp++;
			}
			
			if (!MIN)
			{
				Ksp--;
			}
			
			if (Ksp > 2)
			Ksp = 0;

			if (!ENT)
			{
				_write(EEP_KSP,Ksp);
				
				if (Ksp == 0 || Ksp == 1)
				{
					tk_data = 0;		// analog
					tk_addr = K_ADDR_KSP;
					tk_repeat = 3;
				}
				else
				{
					tk_data = 1;		// digtrans
					tk_addr = K_ADDR_KSP;
					tk_repeat = 3;
				}
				
				calib_ptr_deep++;
			}
			break;
		}
		case 0x02:
		{
			lcd_home();
			lcd_print("AWP ");
			if (Awp)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				Awp = 1;
			}
			if (!MIN)
			{
				Awp = 0;
			}
			if (!ENT)
			{
				_write(EEP_AWP,Awp);
				if (Awp)
				{
					calib_ptr_deep += 1;
				}
				else
				{
					calib_ptr_deep = 0x05;	// kcl
				}
			}
			break;
		}
		case 0x03:
		{
			lcd_home();
			lcd_print("AL1=");
			fuse1(str,AL1);
			lcd_print(str);
			lcd_print("% ");
			
			if (!PLU)
			AL1++;
			if (!MIN)
			AL1--;
			
			if (AL1 < 180)
			AL1 = 180;
			if (AL1 > 220)
			AL1 = 220;
			
			if (!ENT)
			{
				calib_ptr_deep++;
				write_word(EEP_AL1,AL1);
			}
			break;
		}
		case 0x04:
		{
			lcd_home();
			lcd_print("AL2=");
			fuse1(str,AL2);
			lcd_print(str);
			lcd_print("% ");
			
			if (!PLU)
			AL2++;
			if (!MIN)
			AL2--;
			
			if (AL2 > AL1)
			AL2 = AL1;
			if (AL2 < 180)
			AL2 = 180;
			
			if (!ENT)
			{
				calib_ptr_deep++;
				write_word(EEP_AL2,AL2);
			}
			break;
		}
		case 0x05:
		{
			lcd_home();
			lcd_print("KCL ");
			if (Kcl)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				Kcl = 1;
			}
			if (!MIN)
			{
				Kcl = 0;
			}
			if (!ENT)
			{
				_write(EEP_KCL,Kcl);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x06:
		{
			lcd_home();
			lcd_print("WY-U ");
			if (uout)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				uout = 1;
			}
			if (!MIN)
			{
				uout = 0;
			}
			if (!ENT)
			{
				_write(EEP_UOUT,uout);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x07:
		{
			lcd_home();
			lcd_print("LCD ");
			if (lcdmode)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("TIME");
			}
			if (!PLU)
			{
				lcdmode = 1;
			}
			if (!MIN)
			{
				lcdmode = 0;
			}
			if (!ENT)
			{
				_write(EEP_LCDM,lcdmode);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x08:
		{
			lcd_home();
			lcd_print("ZAPISANE");
			WAITMS(2000);
			Pro = PRO_WORK;
			intro();
			break;
		}
	}
	
	if (!PLU || !MIN)
	{
		WAITMS(100);
	}
	
	while( !ENT);
	
}

